package com.xlh.support.experimentshare.controller;

import com.xlh.common.Result;
import com.xlh.support.experimentshare.po.ShareConfigUpdatePO;
import com.xlh.support.experimentshare.service.ExperimentShareConfigService;
import com.xlh.support.experimentshare.vo.ShareConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author cheer
 */
@Api("实验分享控制")
@RestController
@RequestMapping("/experiment/share")
public class ExperimentShareConfigController {

    @Autowired
    private ExperimentShareConfigService experimentShareConfigService;

    @ApiOperation("查询分享控制")
    @GetMapping("/{chapterId}/config")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<ShareConfigVO> queryShareConfig(@PathVariable("chapterId") Long chapterId) {
        ShareConfigVO shareConfigVO = experimentShareConfigService.getShareConfig(chapterId);
        return Result.success(shareConfigVO);
    }

    @ApiOperation("保存分享控制")
    @PostMapping("/{chapterId}/config")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result updateShareConfig(@PathVariable("chapterId") Long chapterId,
                                    @RequestBody @Valid ShareConfigUpdatePO shareConfigUpdatePO) {
        experimentShareConfigService.updateShareConfig(chapterId, shareConfigUpdatePO);
        return Result.success();
    }
}
